package com.jinlia.show.modules.wxapp.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    // 当前跨域请求最大有效时长。这里默认1天
    private static final long MAX_AGE = 24 * 60 * 60;
    /**
     * 上传文件夹
     */
    @Value("${file.path}")
    private String uploadedFolder;

    /**
     * 添加资源处理程序
     * https://blog.csdn.net/qq_43544074/article/details/147576462
     *
     * @param registry 注册表
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 配置资源映射
         * 意思是：如果访问的资源路径是以“/model/”开头的，
         * 就映射到本机的“D:/home/software/data/upload/”这个文件夹内，然后访问资源
         * 注意：D:/home/software/data/upload/ 地址后面的 / 必须添加
         */
        registry.addResourceHandler("/model/**")
                .addResourceLocations("file:" + uploadedFolder);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(MAX_AGE);
    }

}
